package org.gthelper.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;

public class UserInfo {

	private String userID="";
	private int status_login=UserinfoStatus.NotLogin;
	private String sha_pw="";
	private String publickey_user="";
	private String privatekey_server="";
	private Timestamp lastcalltime;
	
	private UserInfo()
	{
		
	}
	private UserInfo(String userID)
	{
		this.userID = userID;
	}
	
	public void setstatus_login(int status_login)
	{
		this.status_login=status_login;
	}
	public int getstatus_login()
	{
		return this.status_login;
	}
	
	public void setpublickey_user(String publickey_user)
	{
		this.publickey_user=publickey_user;
	}
	public String getpublickey_user()
	{
		return publickey_user;
	}
	public void setprivatekey_server(String privatekey_user)
	{
		this.privatekey_server=privatekey_user;
	}
	public void setshapw(String pw)
	{
		this.sha_pw=pw;
	}
	public String getprivatekey_server()
	{
		return this.privatekey_server;
	}
	public void setlastcalltime(Timestamp lastcallTime)
	{
		this.lastcalltime=lastcallTime;
	}
	public Timestamp getlastcalltime()
	{
		return this.lastcalltime;
	}
	
	public String getuserID()
	{
		return this.userID;
	}
	
	public String getshapw()
	{
		return this.sha_pw;
	}
	/*
	 * 通过userID来获取改用户对应的个人信息类
	 */
	public static UserInfo getUserInfobyID(String userID) throws Exception
	{
		UserInfo resultUserInfo = new UserInfo(userID);
		String sqlString = "select * from UserInfo where UserInfo.userID='"+userID+"';";
		Connection connection = DBHandler.getconnection(); 
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(sqlString);
		while(rs.next())
		{
			resultUserInfo.setstatus_login(rs.getInt("status_login"));
			resultUserInfo.setpublickey_user(rs.getString("publickey_user"));
			resultUserInfo.setprivatekey_server(rs.getString("privatekey_server"));
			resultUserInfo.setlastcalltime(Timestamp.valueOf(rs.getString("lastcalltime")));
			resultUserInfo.setshapw(rs.getString("sha_pw"));
		}
		statement.close();
		connection.close();
		return resultUserInfo;
	}
	
	public static boolean idIsExist(String userID) throws Exception
	{
		String sqlString = "select * from UserInfo where UserInfo.userID='"+userID+"';";
		Connection connection = DBHandler.getconnection(); 
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(sqlString);
		if(rs.next())return true;
		else return false;
	}
	
	/*
	 * 更新数据库中的数据
	 */
	public boolean UpdateUserInfo() 
	{
		String sqlString = "update UserInfo set status_login="+this.getstatus_login()
				+ ",publickey_user="+"'"+this.getpublickey_user()+"'"
				+ ",privatekey_server="+"'"+this.getprivatekey_server()+"'"
				+ ",lastcalltime="+DBHandler.getform_datetime(this.getlastcalltime().toString())
				+ ",sha_pw="+"'"+this.sha_pw+"'"
				+ " where userID='"+this.getuserID()
				+ "'; ";

		try{
		Connection con = DBHandler.getconnection();
		Statement statement = con.createStatement();
		statement.executeUpdate(sqlString);
		statement.close();
		con.close();
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	
	/*
	 * 预注册新用户
	 * 在这里被添加的用户是想要注册的，
	 * 如果正式注册，则更新密码和状态值
	 * 如果超过30分钟没有注册，则可以接受新的注册并更改私钥
	 */
	public static boolean insertNewUser(String userID,String prikey) 
	{
		String sqlString = "insert into UserInfo(userID,status_login,privatekey_server,lastcalltime) values("
				+ "'"+userID+"',"
				+UserinfoStatus.Login+","
				+"'"+prikey+"',"
				+DBHandler.getform_datetime((new Timestamp(System.currentTimeMillis())).toString())
				+");";
		try{
			Connection connection = DBHandler.getconnection();
			Statement statement = connection.createStatement();
			statement.execute(sqlString);
			statement.close();
			connection.close();
			return true;
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		
	}
}
